auto()
var storage = storages.create("testWechat");
var 循环次数 = parseInt(storage.get("循环次数", 10)) //一组数量
var 页面等待 = parseInt(storage.get("页面等待", 20)) //页面等待
var 飞行等待 = parseInt(storage.get("飞行等待", 2)) //页面等待
var 切号等待 = parseInt(storage.get("切号等待", 30)) //切号等待
var deviceWidth = device.width
var deviceHeight = device.height
var lastY1 = storage.get("lastY1", 255)
var wy1 = lastY1;
var isStart = false
log("开始辅助")
// 创建悬浮窗
var window = floaty.window(
    <vertical>
        <text id="tipText" text="" w="80dp" h="auto" textSize="12sp" textColor="red" />
        <button id="toggleButton" text="展开" textSize="14sp" w="auto" h="auto" />
        <vertical id="menu" visibility="gone">
            <button id="startButton" text="开始" textSize="14sp" w="auto" h="auto" />
            <button id="stopButton" text="结束" textSize="14sp" w="auto" h="auto" />
        </vertical>
    </vertical>
);
var isOpen = false
// 设置悬浮窗位置
resetWindow()
showTip("按住我可拖动")
stopAllEngines()

window.tipText.setOnTouchListener(function (view, event) {
    switch (event.getAction()) {
        case event.ACTION_DOWN:
            Y1 = event.getRawY();
            return true;
        case event.ACTION_MOVE:
            y1 = event.getRawY() - Y1;
            lastY1 = wy1 + y1
            resetWindow()
            return true;
        case event.ACTION_UP:
            wy1 += y1;
            return true;
    }
    return true;
});


function resetWindow() {
    ui.run(function () {
        window.setPosition(0, lastY1)
        storage.put("lastY1", lastY1)

    });

}

function stopAllEngines() {
    log("我的引擎" + engines.myEngine())
    engines.all().forEach(item => {
        if (item.id != engines.myEngine().id) {
            log("停止引擎" + item)
            item.forceStop()
        }

    })
}

function hideMenu() {
    isOpen = false
    window.menu.setVisibility(8);
    window.toggleButton.setText("展开");
}

function expandOrHideMenu() {
    isOpen = !isOpen
    if (isOpen) {
        window.menu.setVisibility(0);
        window.toggleButton.setText("收起");
    } else {
        window.menu.setVisibility(8);
        window.toggleButton.setText("展开");
    }
}
// 切换按钮点击事件
window.toggleButton.click(() => {
    expandOrHideMenu()

});

// 开始按钮点击事件
window.startButton.click(() => {
    hideMenu()
    threads.start(function () {
        log("开始")
        isStart = true
        main()
    });

});
// 结束按钮点击事件
window.stopButton.click(() => {
    isStart = false
    stopAllEngines()
    threads.shutDownAll()
    toastLog("已停止");
    showTip("已停止")
    hideMenu()
});

function showTip(text) {
    ui.run(() => {
        window.tipText.setText(text);
        log(text)
    })
}
var allData = null
try {
    var path = "/sdcard/链接.txt"; //txt文本路径
    var reg = /^\s+|\s+$/g; //匹配无效空白行
    var allData = files.read(path).replace(reg, "").replace("\r","").split("\n");
    showTip("链接总数" + allData.length)
} catch (e) {
    showTip("链接文件不存在")
    exit()
}
function airMode(isAir) {
    if (isAir) {
        shell("settings put global airplane_mode_on 1", true)
        shell("am broadcast -a android.intent.action.AIRPLANE_MODE --ez state true", true)
    } else {
        shell("settings put global airplane_mode_on 0", true)
        shell("am broadcast -a android.intent.action.AIRPLANE_MODE --ez state false", true)
    }

}
function openAirMode() {
    showTip("开启飞行模式")
    airMode(true)
    sleep(飞行等待*1000)
    airMode(false)
    showTip("关闭飞行模式")
}

// START u0 {flg=0x20000000 cmp=com.tencent.mm/.plugin.webview.ui.tools.MMWebViewUI (has extras)} from uid 10292 from pid 26433 callingPackage com.tencent.mm

//app.launchPackage("com.tencent.mm")
function viewUrl(data) {
    showTip("浏览" + data+"")
    try {
        app.startActivity({
            packageName: "com.tencent.mm",
            className: "com.tencent.mm.plugin.webview.ui.tools.MMWebViewUI",
            data: data,
            root: true
        });
    } catch (e) {
        toastLog("启动微信失败请检查root权限" + e)
        return false
    }

    for (let index = 0; index < 页面等待; index++) {
        if (!isStart) {
            break
        }
        showTip("正在浏览" + index)
        sleep(1000)
    }

    return true



}
function 点击控件(控件) {
    click(控件.bounds().centerX(), 控件.bounds().centerY())
}
function 切换账号() {
    try {
        app.startActivity({
            packageName: "com.tencent.mm",
            className: "com.tencent.mm.plugin.setting.ui.setting.SettingsSwitchAccountUI",
            root: true
        });
    } catch (e) {
        toastLog("切换账号失败" + e)
        return false
    }

    sleep(2000)
    var foundChild = false
    for (let k = 0; k < 10; k++) {
        if (!isStart) {
            break
        }
        let 所有用户 = id("dn").find()
        for (let index = 0; index < 所有用户.length; index++) {
            if (!isStart) {
                break
            }
            let 用户 = 所有用户[index];
            let 孩子们 = 用户.parent().parent().children()
            if (孩子们.length == 2) {
                showTip("点击" + 用户.getText())
                openAirMode()
                点击控件(用户)
                foundChild = true
                return true
            }

        }
        showTip("继续查找用户" + k)
        sleep(1000)
    }

    return false


}
function main() {
    for (let m = 0; m < 循环次数; m++) {
        if (!isStart) {
            break
        }
        showTip("开始循环"+(m+1))
        for (let index = 0; index < allData.length; index++) {
            if (!isStart) {
                break
            }
            let data = allData[index];
            if (viewUrl(data)) {
                if (切换账号()) {
                    for (let k = 0; k < 切号等待; k++) {
                        if (!isStart) {
                            break
                        }
                        showTip("等待切换账号" + k)
                        sleep(1000)
    
                    }
                } else {
                    showTip("切换账号失败！")
                }
    
            }else{
                showTip("浏览失败"+index)
            }
            sleep(1000)
    
        }

        sleep(1000)
    }

    showTip("任务结束")
   
}

function read_delete() {
    var path = "/sdcard/链接.txt"; //txt文本路径
    var reg = /^\s+|\s+$/g; //匹配无效空白行
    var txt = files.read(path).replace(reg, "").split("\r\n");
    let ret_text = txt[0];
    if (txt && txt != "") {
        txt.splice(0, 1);
        files.write(path, txt.join("\r\n"));
        if (ret_text.length > 0) {
            return ret_text.trim();
        }
    } else {
        return null;
    }
}


setInterval(function () { }, 1000)

//com.tencent.mm/com.tencent.mm.plugin.setting.ui.setting.SettingsSwitchAccountUI

